/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.demo.widget.meis.slice;

import com.demo.widget.ResourceTable;

import com.demo.widget.meis.scale.AppPhotoDragHelper;
import com.demo.widget.meis.scale.ScaleImage;
import com.meis.widget.photodrag.PhotoDragRelativeLayout;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.utils.Matrix;
import ohos.app.Context;
import ohos.global.configuration.Configuration;
import ohos.global.resource.ResourceManager;

import java.util.Optional;

public class MeiPhotoDragAbilitySlice extends AbilitySlice {
    private PhotoDragRelativeLayout mPdrLayout;
    private ScaleImage mImage;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_mei_photo_drag);
        mPdrLayout = (PhotoDragRelativeLayout) findComponentById(ResourceTable.Id_pdr_content);
        mPdrLayout.setDragListener(new AppPhotoDragHelper().setOnDragListener(
                new AppPhotoDragHelper.AppOnDragListener() {
            @Override
            public void onAlpha(float alpha) {
                mPdrLayout.setAlpha(alpha);
            }

            @Override
            public Component getDragView() {
                return mImage;
            }

            @Override
            public void onAnimationEnd(boolean slop) {
                if (slop && getAbility() != null) {
                    getAbility().terminateAbility();
                    getAbility().setTransitionAnimation(0, 0);
                }
            }

            @Override
            public Matrix getMatrix() {
                return mImage.getMatrix();
            }

            @Override
            public float getOriginY() {
                return mImage.getOriginY();
            }
        }));
        mImage = (ScaleImage) findComponentById(ResourceTable.Id_pdv_photo);
        mImage.setPixelMap(ResourceTable.Media_ic_mei_ripple);
        setDirection(this);
    }

    private void setDirection(Context context) {
        Integer direction = Optional.ofNullable(context)
                .map(Context::getResourceManager)
                .map(ResourceManager::getConfiguration)
                .map(configuration -> configuration.direction)
                .orElse(Configuration.DIRECTION_UNDEFINED);
        //0-竖屏；1-横屏
        mImage.setDirection(direction);
    }
}